import pick from 'lodash.pick'
  
export const JModalMixin = {
  methods: {
    getDictValues(field){
      if(field.dataSourceType == 'DICT' || field.dataSourceType == 'ENUM' || field.dataSourceType == 'TABLE_COLUMN'){
        const ss = field.dataSourceTypeParam.split(".");
        if(ss.length <= 3){            
          return this.dictValues[ss[0]];
        }else{
          return this.dictValues[field.dataSourceTypeParam];
        }
      }
    },
    getDictValue(dict, text){
      if(!text){return;}
      text = text + "";
      const vs = this.dictValues[dict];
      if(vs){
        for(var v =0; v < vs.length; v++){
          if(vs[v].valueCode == text){
            return vs[v].valueName;
          }
        }
      }
    },
    calcScale(scale){
      var d = 1;
      if(scale > 0){
        for(var i=0;i<scale; i++){
          d = d/10;
        }
      }else if(scale < 0){
        scale = scale * 1;
        for(var i=0; i<scale; i++){
          d = d * 10;
        }
      }
      return d;
    },
    setFormItemValue(model, form, columns){
      var cns = [];
      for(var idx in columns){
        if(columns[idx].dataIndex){
          cns.push(columns[idx].dataIndex)
        }
      }
      const data = pick(model, ...cns);
      for(var idx in columns){
        const dst = columns[idx].dataSourceType;
        const key = columns[idx].dataIndex;
        if(dst == "DICT" || dst == "ENUM" || dst == "TABLE_COLUMN" ){
          if(data[key]){
            data[key] = data[key] + '';
          }else{
            delete data[key];
          }
        }
      }
      form.setFieldsValue(data)
    }
  }
}